<title>角色管理</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>系统管理</cite></a>
        <a><cite>角色管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-role-formlist">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="角色名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-role" lay-submit lay-filter="LAY-sys-role-search">
                        搜索
                    </button>
                    <button class="layui-btn layuiadmin-btn-role" data-type="add">
                        添加
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-sys-role-table" lay-filter="LAY-sys-role-table"></table>
            <script type="text/html" id="table-role-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">
                    <i class="layui-icon layui-icon-edit"></i>编辑</a>
                <!--{{#  if(d.name != '超级管理员'){ }}-->
                <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i-->
                        <!--class="layui-icon layui-icon-delete"></i>删除</a>-->
                <!--{{#  } }}-->
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="permission"><i
                        class="layui-icon layui-icon-auz"></i>授权</a>

            </script>
        </div>
    </div>
</div>


<script type="text/html" id="modelRole">
    <form id="modelRoleForm" lay-filter="modelRoleForm" class="layui-form model-form">

        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入角色名称" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色描述</label>
            <div class="layui-input-block">
                <input name="desc" placeholder="请输入角色描述" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>


        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-fluid" lay-filter="modelRoleSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script>
    layui.use(['admin', 'table', 'setter', 'dtree'], function () {
        var $ = layui.$
            // , setter = layui.setter
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , form = layui.form
            , dtree = layui.dtree
            , setter = layui.setter;

        var apiUrl = setter.apiUrl;
        //管理员管理
        table.render({
            elem: '#LAY-sys-role-table'
            , url: apiUrl + '/mapi/permission/r/list'//'
            , method: 'post'
            , page: true
            , contentType: 'application/json'
            , headers: {'token': layui.data(setter.tableName).token}
            , request: {
                pageName: 'pageNo'
                , limitName: 'pageSize'
            }
            , response: {
                statusCode: 0, //规定数据状态的字段名称，默认：code
            }
            , parseData: function (res) {
                if (res.code === 0) {
                    //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                    };
                }
            }
            , cols: [[
                // {type: 'checkbox', fixed: 'left'}
                {field: 'id', title: '角色id'}
                , {field: 'name', title: '角色名称'}
                , {field: 'desc', title: '角色描述'}
                , {title: '操作', fixed: 'right', toolbar: '#table-role-admin'}
            ]]
            // , done: function () {
            // $("[data-field='id']").css('display', 'none');
            // }
            , text: {
                none: '未检索到数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        });


        //监听搜索
        form.on('submit(LAY-sys-role-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-sys-role-table', {
                where: field
            });
        });

        // 工具条点击事件
        table.on('tool(LAY-sys-role-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'permission') { // 添加
                admin.popup({
                    title: '角色赋权'
                    , area: ['50%', '80%']
                    , id: 'LAY-popup-role-permission'
                    , success: function (layero, index) {
                        view(this.id).render('system/permission/rolepermission', data);
                    }
                });
            }
        });

        function showEditModel(curData) {
            admin.popup({
                title: '编辑角色'
                // , area: ['420px', '430px']
                , id: 'LAY-popup-role-add'
                , content: $('#modelRole').html()
                , success: function (layero, index) {
                    form.val('modelRoleForm', curData);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelRoleSubmit)', function (data) {
                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                        admin.req({
                            url: apiUrl + '/mapi/permission/r/update/' + curData.id
                            , method: 'put'
                            , data: JSON.stringify(data.field)
                            , done: function (response) {
                                if (response.code === 0) {
                                    layer.msg('保存成功', {
                                        offset: '15px', icon: 1, time: 1000
                                    }, function () {
                                        layer.close(index); //执行关闭
                                        layui.table.reload('LAY-sys-role-table'); //重载表格
                                    });
                                } else {
                                    layer.msg(response.msg, {
                                        offset: '15px', icon: 2
                                    });
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        }

        //事件
        var active = {
            add: function () {
                var checkStatus = table.checkStatus('LAY-sys-role-table'), curData = checkStatus.data; //得到选中的数据
                admin.popup({
                    title: '添加角色'
                    // , area: ['420px', '430px']
                    , id: 'LAY-popup-role-add'
                    , content: $('#modelRole').html()
                    , success: function (layero, index) {
                        // 表单提交事件
                        form.on('submit(modelRoleSubmit)', function (data) {
                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            admin.req({
                                url: apiUrl + '/mapi/permission/r/add'
                                , method: 'post'
                                , data: JSON.stringify(data.field)
                                , done: function (response) {
                                    if (response.code === 0) {
                                        layer.msg('保存成功', {
                                            offset: '15px', icon: 1, time: 1000
                                        }, function () {
                                            layer.close(index); //执行关闭
                                            layui.table.reload('LAY-sys-role-table'); //重载表格
                                        });
                                    } else {
                                        layer.msg(response.msg, {
                                            offset: '15px', icon: 2
                                        });
                                    }
                                }
                            });
                            return false;
                        });
                    }
                });
            }
        }


        $('.layui-btn.layuiadmin-btn-role').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>